<?php
/*--------------------------
	file:	form.class
	intro:	表单设计类
--------------------------*/
class Form extends Base{
/*-------------------------------------------
*	表单类 form.class
*	Author: corrie
*	生成表单布局
--------------------------------------------*/
	var $_formdata;
	var $_forminfo;
	var $_formarray;
	var	$_index = 1;
	var $_signer = 1;
	var $_token;
	var $_html;
	var $_linenum = 1;
	var $_muti = 1;

	function Form(){
		$this->_set();
	}
	
	function _set(){
		$_formname = 'field';
		$_formprename = 'prefield';
	}
	
	function debug(){
		$this->_debugshow($this->_sprint($this->_formarray));
	}
	
	function _reset(){
		$this->_formdata = "";
		$this->_forminfo = array();
		$this->_formarray = array();
		$this->_index = 1;
		$this->_signer = 1;
		$this->_linenum = 1;
	}
	
	function signer($index){
		$this->_signer = $index;
	}
	
	//createelment(类型, name名称, 其他, 值, 附加数据, 标记)
	function createelment($type, $name, $value='', $add='', $list='', $signer=''){	
		if(!$signer || $signer=='')$signer=$this->_signer;
		$attrname = "name='field[$signer]{$name}'";
		$preattrname = "name='prefield[$signer]{$name}'";
		$value && $preelement = "<input type='hidden' {$preattrname} value='{$value}' />";
		!$value && $preelement = '';
		switch($type){
			case "text":
				$element = "{$list}<input type='text' {$attrname} value='{$value}' {$add} />";
			break;
			case "password":
				$element = "{$list}<input type='password' {$attrname} value='{$value}' {$add} />";
			break;
			case "textarea":
				$element = "<textarea {$attrname} {$add}>{$value}</textarea>";
			break;
			case "file":
				$preelement = '';
				$element = "<input type='file' {$attrname} {$add} />";
			break;
			case "submit":
				$preelement = '';
				$element = "<input type='submit' {$attrname} value='{$value}' {$add} />";
			break;
			case "reset":
				$preelement = '';
				$element = "<input type='reset' {$attrname} value='{$value}' {$add} />";
			break;
			case "button":
				$preelement = '';
				$element = "<input type='button' {$attrname} value='{$value}' {$add} />";
			break;
			case "checkbox":
				$checked = '';
				if(!$list){
					$key = '1';
					$item = '';
				}else{
					list($key, $item) = explode(',', $list);
					if($key==$value){
						$checked = "checked='true'";
					}
				}
				$element = "<input type='checkbox' {$attrname} value='{$key}' {$checked} {$add} />{$item}";
			break;
			case "radio":
				$checked = '';
				if(!$list){
					$key = '1';
				}else{
					list($key, $item) = explode(',', $list);
					if($key==$value){
						$checked = "checked='true'";
					}
				}
				$element = "<input type='radio' {$attrname} value='{$key}' {$checked} {$add} />{$item}";
			break;
			case "select":
				if(!is_array($list)){
					if(!$list){
						$key = '0';
						$item = "No Items!";
					}else{
						list($key, $item) = explode(',', $list);
					}
					$element = "<select {$attrname} {$add}><option value='{$key}'>{$item}</option></select>";
				}else{
					$element = "<select {$attrname} {$add}>";
					foreach($list as $key=>$item){
						$element.= "<option value='{$key}'>{$item}</option>";
					}
					$element.= "</select>";
				}
			break;				
		}
		return $preelement.$element;
	}
	
	function createone($type, $name, $value='', $add='', $list='', $signer=''){	
		if(strexists($name, '[')){
			$realname = substr($name, 0, strpos($name, '['));
		}else{
			$realname = $name;
		}
		$id = 'id_'.$realname;
		
		if(is_array($list)){
			$one = '';
			switch($type){
				case "checkbox":
					$this->_muti = 1;
					foreach($list as $k=>$v){
						$ename = str_replace('');
						$id = "id_{$realname}_".$this->_muti;
						$nadd = $add." id='{$id}'";
						$one.= "<label for='{$id}' class='form_{$type}'>".$this->createelment($type, $name, $value, $nadd, "{$k},{$v}", $signer)."</label>";
					}
				break;
				case "radio":
					$this->_muti = 1;
					foreach($list as $k=>$v){										
						$id = "id_{$realname}_".$this->_muti;
						$nadd = $add." id='{$id}'";
						$one.= "<label for='{$id}' class='form_{$type}'>".$this->createelment($type, $name, $value, $nadd, "{$k},{$v}", $signer)."</label>";
						$this->_muti++;
					}
				break;
				case "select":
					$one = "<label for='none' class='form_{$type}'>".$this->createelment($type, $name, $value, $add, $list, $signer)."</label>";
				break;
				case "text":
					$one = '';
					foreach($list as $k=>$v){
						if(is_array($value)){
							$value = $value[$k];
						}
						$one.= "<label for='none' class='form_{$type}'>".$this->createelment($type, $name, $value, $add, "{$v}", $signer)."</label> ";
					}
				break;
				default: $one = "<label for='none' class='form_{$type}'>".$this->createelment($type, $name, $value, $add, $list, $signer)."</label>";
			}
		}else{
			$one = "<label for='none' class='form_{$type}'>".$this->createelment($type, $name, $value, $add, $list, $signer)."</label>";
		}
		return $one;
	}
	
	function addgrid($css, $content, $add=''){
		$html = "<div class='form_grid {$css}' {$add}><div class='midp_bd'>";
		$html.= $content;
		$html.= "</div></div>";
		return $html;
	}
	
	function addline(){
		$args = func_get_args();
		$args[0] ? ' '.$args[0] : '';
		$addcss = "";
		if($this->_linenum%2==0){
			$addcss = " form_odd";
		}
		$html = "<div class='form_line{$addcss}'{$args[0]}>";
		unset($args[0]);
		$html.= join(' ', $args);
		$html.= "<div class='clear'></div></div>\n";
		$this->_linenum ++;
		return $html;
	}
	
	function addgroup(){
		$args = func_get_args();
		$args[0] ? ' '.$args[0] : '';
		$html = "<div class='form_group'{$args[0]}>";
		unset($args[0]);
		$html.= join(' ', $args);
		$html.= "</div>\n";
		return $html;
	}
	
	function addplat(){
		$args = func_get_args();
		$args[0] ? ' '.$args[0] : '';
		$html = "<div class='form_plat'{$args[0]}>";
		unset($args[0]);
		$html.= join(' ', $args);
		$html.= "</div>\n";
		return $html;
	}
	
	function addform(){
		$args = func_get_args();
		$html = "<form {$args[0]}>\n";
		unset($args[0]);
		$html.= join(' ', $args);
		$html.= "</form>\n";
		return $html;
	}
	
	function addhtml(){
		$args = func_get_args();
		//unset($args[0]);
		$html = join(' ', $args);
		$this->_html = $html;
		//return $html;
	}
	
	function addtoken(){
		$this->_token = 1;
	}
}
?>